package kevin.workplan.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import kevin.lib.util.JsonUtil;
import kevin.workplan.database.dao.CPlanMapper;
import kevin.workplan.database.dao.CProjectsMapper;
import kevin.workplan.database.gen.model.Plan;
import kevin.workplan.database.gen.model.PlanExample;
import kevin.workplan.database.gen.model.Projects;
import kevin.workplan.database.gen.model.ProjectsExample;
import kevin.workplan.exception.BusinessException;
import kevin.workplan.exception.ServiceException;
import kevin.workplan.service.PlanService;
import kevin.workplan.util.Util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("planService")
public class PlanServiceImpl implements PlanService {
	private static final Logger log = LoggerFactory.getLogger(PlanServiceImpl.class);
	
	@Autowired
	private CPlanMapper planMapper;
	@Autowired
	private CProjectsMapper projectsMapper;

	@Override
	public void createPlan(Plan plan) throws BusinessException,
			ServiceException {
		try {
			planMapper.insertSelective(plan);
		} catch (Exception e) {
			log.error("create plan error. plan:" + JsonUtil.toJson(plan), e);
			throw new ServiceException("create plan error.", e);
		}
	}

	@Override
	public void editPlan(Plan plan) throws BusinessException, ServiceException {
		try {
			planMapper.updateByPrimaryKeySelective(plan);
		} catch (Exception e) {
			log.error("edit plan error. plan:" + JsonUtil.toJson(plan), e);
			throw new ServiceException("edit plan error.", e);
		}
	}

	@Override
	public List<Plan> search(PlanExample px, int curpage, int limit, String orderBy) throws BusinessException,
			ServiceException {
		try {
			px.setOffset((curpage-1)*limit);
			px.setLimit(limit);
			if(!Util.empty(orderBy)) {
				px.setOrderByClause(orderBy);
			}
			return planMapper.searchByExampleWithBLOBs(px);
		} catch (Exception e) {
			log.error("search plan error. plan:" + JsonUtil.toJson(px), e);
			throw new ServiceException("search plan error.", e);
		}
	}

	@Override
	public int searchCount(PlanExample px) throws BusinessException,
			ServiceException {
		try {
			return planMapper.countByExample(px);
		} catch (Exception e) {
			log.error("searchCount plan error. plan:" + JsonUtil.toJson(px), e);
			throw new ServiceException("searchCount plan error.", e);
		}
	}

	@Override
	public Plan getById(Long id) throws BusinessException, ServiceException {
		try {
			return planMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			log.error("getById plan error. id:" + id, e);
			throw new ServiceException("getById plan error.", e);
		}
	}

	@Override
	public List<Projects> getProjects(Long uid, int type) throws BusinessException, ServiceException {
		try {
			ProjectsExample ex = new ProjectsExample();
			ProjectsExample.Criteria cr = ex.createCriteria();
			cr.andUidEqualTo(uid);
			cr.andTypeEqualTo(type);
			return projectsMapper.selectByExample(ex);
		} catch (Exception e) {
			log.error("getProjects plan error. uid:" + uid, e);
			throw new ServiceException("getProjects plan error.", e);
		}
	}

	@Override
	public List<Projects> getChildProjects(Long uid, long pid)
			throws BusinessException, ServiceException {
		try {
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("uid", uid);
			map.put("pid", pid);
			return projectsMapper.getChildProjects(map);
		} catch (Exception e) {
			log.error("getChildProjects plan error. uid:" + uid, e);
			throw new ServiceException("getChildProjects plan error.", e);
		}
	}

}
